package com.ctshk.sapp.train.controller;


import com.ctshk.common.annotations.Authority;
import com.ctshk.common.annotations.OperationLogAnnotation;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.OperationLogModel;
import com.ctshk.common.enums.OperationLogType;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.utils.individual.conversion.BatchStrategyReq;
import com.ctshk.common.utils.individual.conversion.dto.BatchStrategyDto;
import com.ctshk.common.web.support.BaseController;
import com.ctshk.rpc.train.dto.TrainProductDetailDTO;
import com.ctshk.rpc.train.dto.TrainProductManagePageDTO;
import com.ctshk.rpc.train.dto.TrainProductStrategyDTO;
import com.ctshk.rpc.train.req.TrainProductPageReq;
import com.ctshk.rpc.train.req.TrainProductStrategySaveBatchReq;
import com.ctshk.rpc.train.req.TrainProductStrategySaveReq;
import com.ctshk.rpc.train.service.ITrainProductStrategyService;
import com.ctshk.sapp.train.convertor.Convertor;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 火车票产品加幅策略 前端控制器
 * </p>
 *
 * @author 彭柳霖
 * @since 2021-03-15
 */
@Api(value="产品中心-火车上架设置接口", tags="产品中心-火车上架设置接口")
@RestController
@RequestMapping("/train-product-strategy")
public class TrainProductStrategyController extends BaseController {


    @DubboReference
    private ITrainProductStrategyService trainProductStrategyService;


    @ApiOperation(value = "产品中心-火车票管理列表查询", notes = "产品中心-火车票管理列表查询")
    @PostMapping(value = "/query-list")
    @Authority
    public Result<PageResponse<TrainProductManagePageDTO>> queryList(@RequestBody @Valid TrainProductPageReq trainProductPageReq) {
        TokenUser currentUser = getCurrentUser();
        trainProductPageReq.setCurrentUserId(currentUser.getId());
        return Result.success(trainProductStrategyService.queryList(trainProductPageReq));
    }

    @ApiOperation(value = "火车票加幅策略-保存加幅策略", notes = "火车票加幅策略-保存加幅策略")
    @OperationLogAnnotation(operModul = OperationLogModel.TICKET, operType = OperationLogType.ADD, operTitle = "火车票加幅策略-保存加幅策略", operDesc = "火车票加幅策略-保存加幅策略")
    @PostMapping("/save")
    public Result save(@Valid @RequestBody TrainProductStrategySaveReq trainProductStrategySaveReq) {
        Long userId = getCurrentUser(getRequest()).getId();
        return trainProductStrategyService.save(trainProductStrategySaveReq.getTrainProductId(), trainProductStrategySaveReq.getTrainProductStrategyReqList(), userId);
    }


    @ApiOperation(value = "火车票加幅策略-批量保存加幅策略", notes = "火车票加幅策略-批量保存加幅策略")
    @OperationLogAnnotation(operModul = OperationLogModel.TICKET, operType = OperationLogType.ADD, operTitle = "火车票加幅策略-批量保存加幅策略", operDesc = "火车票加幅策略-批量保存加幅策略")
    @PostMapping("/save/batch")
    public Result saveBatch(@RequestBody @Valid BatchStrategyReq batchStrategyReq) {
        Long userId = getCurrentUser(getRequest()).getId();
        TrainProductStrategySaveBatchReq trainProductStrategySaveBatchReq = Convertor.frontToTrainProduct(batchStrategyReq);
        return trainProductStrategyService.saveBatch(trainProductStrategySaveBatchReq, userId);
    }

    @ApiOperation(value="修改时-根据产品id查询产品加幅详情信息", notes="修改时-根据产品id查询产品加幅详情信息")
    @OperationLogAnnotation(operModul = OperationLogModel.TICKET, operType = OperationLogType.QUERY, operTitle = "根据产品id查询产品加幅详情信息", operDesc = "根据产品id查询产品加幅详情信息")
    @GetMapping("/queryByProductId")
    public Result<BatchStrategyDto> queryByProductId(@RequestParam(value = "productId", required = true) Long productId) {
        List<TrainProductStrategyDTO> list = trainProductStrategyService.queryByProductId(productId);
        return Result.success(Convertor.TrainProductToFront(list));
    }

    @ApiOperation(value = "火车票-产品中心-查看详情", notes = "火车票-产品中心-查看详情)")
    @OperationLogAnnotation(operModul = OperationLogModel.TICKET, operType = OperationLogType.QUERY, operTitle = "火车票-产品中心-查看详情", operDesc = "火车票-产品中心-查看详情")
    @GetMapping("/detail/{id}")
    public Result<TrainProductDetailDTO> getDetail(@PathVariable("id") Long productId) {
        return trainProductStrategyService.selectDetail(productId);
    }

}
